<template>
    <div>
        <!-- <home-header :city="city"></home-header> -->
        <home-header></home-header>
        <home-swiper :swiperList="swiperList"></home-swiper>
        <home-icons :iconList="iconList"></home-icons>
        <home-recommend :recommendList="recommendList"></home-recommend>
        <home-weekend :weekendList="weekendList"></home-weekend>
    </div>
</template>
<script>
import HomeHeader from './components/Header'
import HomeSwiper from './components/swiper'
import HomeIcons from './components/icons'
import HomeRecommend from "./components/recommend"
import HomeWeekend from "./components/weekend"
// ajax 安裝axios   npm install axios --save
import axios from "axios"
import { mapState } from "vuex"
export default {
  name: 'Home',
  components:{
      HomeHeader: HomeHeader,
      HomeSwiper: HomeSwiper,
      HomeIcons,
      HomeRecommend,
      HomeWeekend
  },
  data () {
      return {
          // city:'',
          swiperList:[],
          iconList:[],
          recommendList:[],
          weekendList:[],
          lastCity:''
      }
  },
  computed:{
      ...mapState(['city'])
  },
  methods:{
      getHomeInfo () {
          // 上線之後 恢复成
          // 1.axios.get("/api/index.json")
          // 实际
          // 2.axios.get("/static/mock/index.json")
          // 用1替代2的方法 在config 下的 index.js 文件里  
          axios.get("/api/index.json?city="+ this.city)
            .then(this.getHomeInfoSucc)
      },
      getHomeInfoSucc (res) {
        // console.log(res.data)
        // json字符串 转换为 data
        // var data = eval("("+res.data+")");//转对象也没问题(仍是对象)
        // var data = res.data.parseJSON();
        // var data = JSON.parse(res.data);
        // var data = $.parseJSON(res.data)
        res = res.data;
        // json 注释注意 末尾逗号也应注意 
        if(res.ret && res.data){
            const data = res.data
            // this.city = data.city
            this.swiperList = data.swiperList
            this.iconList = data.iconList
            this.recommendList = data.recommendList
            this.weekendList = data.weekendList
        } 
      }
  },
  mounted () {
      console.log('mounted');
      this.lastCity = this.city
      this.getHomeInfo()
  },
  //使用 keep-alive 多出一个钩子
  activated(){
      console.log('activated')
      if(this.lastCity !== this.city){
          this.lastCity = this.city
          this.getHomeInfo()
      }


  }
}
</script>
<style scoped>
</style>
